import Api_wallet from '../../common/api/wallet.js'

const state = {
	//钱包列表
	get walletList(){
		return uni.getStorageSync('walletList')
	},
	set walletList(value){
		uni.setStorageSync('walletList',value)
	},
	waitCount:0,//待付款数量
	waitList:[],//待付款列表
	isRefreshWaitCount:false,//是否刷新待付款数量
}
const mutations = {
	SET_WALLET(state,payload){
		state.walletList = payload
	},
	SET_BALANCE(state,payload){
		let walletList = state.walletList
		for(let i of walletList){
			if(i.id == payload.id){
				i.balance = payload.balance
			}
		}
		state.walletList = walletList
	},
	SET_WAITCOUNT(state,payload){
		state.waitCount = payload
	},
	SET_ISREFRESHWAITCOUNT(state,payload){
		state.isRefreshWaitCount = payload
	},
	SET_WAITLIST(state,payload){
		state.waitList = payload
	}
}
const actions = {
	//钱包列表
	wallet({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.wallet(payload).then(res => {
				commit('SET_WALLET',res)
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//钱包支付
	walletPay({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.walletPay(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//钱包开户
	walletOpen({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.walletOpen(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//查询开户状态
	walletSearch({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.walletSearch(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//查询钱包余额
	walletBalance({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.walletBalance(payload).then(res => {
				commit('SET_BALANCE',{balance:res.data.balance,id:res.data.id})
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//钱包管理
	walletManage({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.walletManage(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//我的订单
	order({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.order(payload).then(res => {
				if(payload.status == 1){
					commit('SET_WAITCOUNT',res.data.total)
					commit('SET_WAITLIST',res.data.data)
				}
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//卖出记录
	orderSells({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.orderSells(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//创建订单
	orderBuy({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.orderBuy(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//支付
	orderPay({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.orderPay(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//支付完成
	orderPaid({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.orderPaid(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//取消支付
	orderCancel({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.orderCancel(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//订单查询
	orderShow({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.orderShow(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//快速购买
	buyQuickly({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.buyQuickly(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//购买一个
	buyOne({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.buyOne(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//购买盲盒
	buyBox({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_wallet.buyBox(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
